package DDL;

import java.util.ArrayList;
import java.util.List;

import util.Methods;
import DuiXiang.TableModel;

public class EditTableParsing {
	private static boolean bChar=false;

	public static int editTableParsing(String sqlSentence) {
		boolean bLeftBracket = false, bRightBracket = false;// 是否有“(”和“)”
		for (int i = 0; i < sqlSentence.length(); i++) {
			if (sqlSentence.charAt(i) == '(')
				bLeftBracket = true;
			else if (sqlSentence.charAt(i) == ')') {
				bRightBracket = true;
				break;
			}
		}
		if (bLeftBracket != true || bRightBracket != true
				|| sqlSentence.charAt(sqlSentence.length() - 1) != ';')// SQL语句是否合法
		{
			System.out.println("sql语句中没有“(”或“)”");
			return -1;//SQL语句不合法
		}
		String[] sqlSentenceFrontArray = sqlSentence.split("\\(");
		String[] sqlFrontArray = sqlSentenceFrontArray[0].trim().split("\\s+");
		String[] sqlSentenceLastArray = sqlSentenceFrontArray[1].split("\\)");
		String[] sqlLastArray = sqlSentenceLastArray[1].split("\\s+");
		if (!(sqlFrontArray[0].equals("EDIT") || sqlFrontArray[0]
				.equals("edit"))) {
			System.out.println("没有关键字edit");
			return -1;//SQL语句不合法
		}
		if (!(sqlFrontArray[1].equals("TABLE") || sqlFrontArray[0]
				.equals("table"))) {
			System.out.println("没有关键字table");
			return -1;//SQL语句不合法
		}
		if (!(sqlLastArray[0].equals("IN") || sqlLastArray[0].equals("in"))) {
			System.out.println("没有关键字in");
			return -1;//SQL语句不合法
		}
		String tableName = sqlFrontArray[2];
		String dataBaseName = sqlLastArray[1].substring(0,
				sqlLastArray[1].length() - 1);
		String[] sqlMiddleArray = sqlSentenceLastArray[0].split(",");
		String[] tableDetailArray;
		List<TableModel> tableDetail = new ArrayList<TableModel>();

		for (int i = 0; i < sqlMiddleArray.length; i++) {
			TableModel tableModel = new TableModel();
			tableDetailArray = sqlMiddleArray[i].trim().split("\\s+");
			switch (tableDetailArray.length) {
			case 3:
				tableModel.setsFieldName(tableDetailArray[0]);

				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if (getSize(tableDetailArray[1]) < 0) {
					System.out.println("类型错误");
					return -2;//类型错误
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if (bChar == false)
					tableModel.setsType(tableDetailArray[1]);
				else {
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar = false;
				}
				tableModel.setbNullFlag('n');
				tableModel.setbValidFlag('y');
				break;
			case 4:
				tableModel.setsFieldName(tableDetailArray[0]);

				if (getSize(tableDetailArray[1]) < 0) {
					System.out.println("类型错误");
					return -2;//类型错误
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if (bChar == false)
					tableModel.setsType(tableDetailArray[1]);
				else {
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar = false;
				}
				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if (tableDetailArray[3].equals("NO_NULL"))
					tableModel.setbNullFlag('n');
				else
					tableModel.setbNullFlag('y');
				tableModel.setbValidFlag('y');
				break;
			case 5:
				tableModel.setsFieldName(tableDetailArray[0]);

				if (getSize(tableDetailArray[1]) < 0) {
					System.out.println("类型错误");
					return -2;//类型错误
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if (bChar == false)
					tableModel.setsType(tableDetailArray[1]);
				else {
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar = false;
				}
				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if (tableDetailArray[3].equals("NO_NULL"))
					tableModel.setbNullFlag('n');
				else
					tableModel.setbNullFlag('y');
				if (tableDetailArray[4].equals("VALID"))
					tableModel.setbValidFlag('y');
				else
					tableModel.setbKey('n');
				break;
			default: {
				System.out.println("sql语句有错");
				return -1;//sql语句不合法
			}
			}
			tableDetail.add(tableModel);
		}
		for(int i=0;i<tableDetail.size();i++){
			if(EditTable.editTable(dataBaseName, tableName, tableDetail.get(i))==false)
				return -3;////edit出错
		}
		return 1;//成功

	}

	public static int getSize(String type) {// 得到字长
		int low = 0, high = 0;
		for (int i = 0; i < type.length(); i++) {
			if (type.charAt(i) == '[')
				low = i;
			else if (type.charAt(i) == ']') {
				high = i;
				break;
			}
		}
		if (type.equals("int"))
			return 4;
		else if (type.equals("byte"))
			return 1;
		else if (type.equals("long"))
			return 8;
		else if (type.equals("char"))
			return 11;
		else if (type.equals("float"))
			return 4;
		else if (type.substring(0, low).equals("char")
				|| type.substring(0, low).equals("varchar")) {
			bChar = true;
			return Integer.parseInt(type.substring(low + 1, high));
		} else
			return -1;
	}

}
